package com.chagee.store.operation.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chagee.base.utils.HttpResponse;
import com.chagee.base.utils.HttpResponseUtil;
import com.chagee.base.utils.PaginationData;
import com.chagee.base.utils.PaginationRequest;
import com.chagee.payment.sdk.utils.AjaxResult;
import com.chagee.store.operation.api.request.*;
import com.chagee.store.operation.api.response.SettlementResponse;
import com.chagee.store.operation.api.response.WorkOrderResponse;
import com.chagee.store.operation.common.constant.ResultConstant;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.facade.StoreAppSettlementFacade;
import com.chagee.store.operation.server.facade.StoreSettlementFacade;
import com.chagee.store.operation.server.model.param.CreateTradeOrderParam;
import com.chagee.store.operation.server.model.vo.PaymentResultVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * APP打烊结算
 *
 * @Author : hk
 * @Date : 2024/5/10
 */

@Slf4j
@RestController
@RequestMapping("/appSettlement")
public class StoreAppSettlementController {

	@Resource
	private StoreAppSettlementFacade storeAppSettlementFacade;

	/**
	 * 打烊结算单
	 *
	 * @param request
	 * @return
	 */
	@PostMapping("/query")
	@ApiOperation("查询结算单表")
	public HttpResponse<PaginationData<SettlementResponse>> query(@RequestBody PaginationRequest<SettlementAppRequest> request) {
		log.info("#StoreSettlementController_query_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		PaginationData<SettlementResponse> data = storeAppSettlementFacade.query(request);
		return HttpResponseUtil.success(data);
	}

	@PostMapping("/queryDetail")
	@ApiOperation("结算单详情")
	public HttpResponse<SettlementResponse> queryDetail(@RequestBody @Valid SettlementAppOrderDetailRequest request) {
		log.info("#StoreSettlementController_queryDetail_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		SettlementResponse settlementResponse = storeAppSettlementFacade.queryDetail(request);
		return HttpResponseUtil.success(settlementResponse);
	}

	@PostMapping("/queryOrderList")
	@ApiOperation("结算单工单详情")
	public HttpResponse<PaginationData<WorkOrderResponse>> queryOrderList(@RequestBody PaginationRequest<SettlementAppOrderDetailRequest> request) {
		log.info("#StoreSettlementController_queryOrderList_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		PaginationData<WorkOrderResponse> data = storeAppSettlementFacade.queryOrderList(request);
		return HttpResponseUtil.success(data);
	}

	/**
	 * 工单确认
	 */
	@PostMapping("/collate")
	@ApiOperation("工单确认")
	public HttpResponse<Boolean> collate(@RequestBody @Valid SettlementCollateRequest request) {
		log.info("#StoreSettlementController_collate_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		return HttpResponseUtil.success(storeAppSettlementFacade.collate(request));
	}

	/**
	 * 去支付接口
	 */
	@PostMapping("/preSubmitOrder")
	@ApiOperation("去支付罚金")
	public HttpResponse<Object> preSubmitOrder(@RequestBody @Valid CreateTradeOrderParam createTradeOrderParam) {
		log.info("#NoticeController.preSubmitOrder#request：{}", JSON.toJSONString(createTradeOrderParam));
		PaymentResultVO resultVO = null;
		try {
			resultVO = storeAppSettlementFacade.preSubmitOrder(createTradeOrderParam);
		} catch (Exception e) {
			return HttpResponseUtil.build(null, BizErrorCode.NOTICE_FAILED.getErrorCode(), BizErrorCode.NOTICE_FAILED.getErrorMsg());
		}
		return HttpResponseUtil.build(resultVO.getData(), resultVO.getCode(), resultVO.getMag());
	}

	/**
	 * 罚金支付完成后修改其支付状态
	 *
	 * @param httpServletRequest 支付回调修改状态入参
	 * @return 支付回调修改状态出参
	 */
	@ApiOperation("罚金支付完成后修改其支付状态")
	@PostMapping(value = "/payNoticeCallback", produces = MediaType.APPLICATION_JSON_VALUE)
	public String payNoticeCallback(HttpServletRequest httpServletRequest) throws Exception {
		return storeAppSettlementFacade.payNoticeCallback(httpServletRequest);
	}


	/**
	 * 门店结算驳回
	 */
	@PostMapping("/rejected")
	@ApiOperation("门店结算驳回")
	public HttpResponse<Boolean> rejected(@RequestBody @Valid SettlementRejectedRequest request) {
		log.info("StoreSettlementController_issued_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		Boolean rejected = storeAppSettlementFacade.rejected(request);
		return HttpResponseUtil.success(rejected);
	}

}






























